package com.hb.study.design_patterns.chain_of_responsibility;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 抽象类
 *
 * @author Mr.Huang
 * @version v0.1, AbstractChain.java, 2020/6/16 10:54, create by huangbiao.
 */
public abstract class AbstractChain {

    private Integer step;

    public AbstractChain(int step) {
        this.step = step;
    }

    public Integer getStep() {
        return step;
    }

    private AbstractChain chain;

    public void setChain(AbstractChain chain) {
        this.chain = chain;
    }

    public AbstractChain getChain() {
        return chain;
    }

    abstract void doChain();

    /**
     * 按step顺序执行
     */
    public static void doChainByStep(AbstractChain... chain) {
        List<AbstractChain> chainList = Arrays.asList(chain);
        List<AbstractChain> sortChainList = chainList.stream().sorted(Comparator.comparing(AbstractChain::getStep)).collect(Collectors.toList());
        for (int i = 0; i < sortChainList.size() - 1; i++) {
            sortChainList.get(i).setChain(sortChainList.get(i + 1));
        }
        sortChainList.get(0).doChain();
    }

}

    